package cn.wbu.zyh.service.impl;

import cn.wbu.zyh.dto.TeamDTO;
import cn.wbu.zyh.dto.TeamMemberDTO;
import cn.wbu.zyh.dto.TeamMemberPageQueryDTO;
import cn.wbu.zyh.entity.Team;
import cn.wbu.zyh.entity.TeamMember;
import cn.wbu.zyh.mapper.TeamMapper;
import cn.wbu.zyh.result.PageResult;
import cn.wbu.zyh.service.TeamService;
import cn.wbu.zyh.vo.TeamVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    private TeamMapper teamMapper;
    @Override
    @Transactional
    public void createTeam(TeamDTO teamDTO) {
        Team team = new Team();
        TeamMember teamMember = new TeamMember();
        //对象属性拷贝
        BeanUtils.copyProperties(teamDTO,team);
        Long UUid = generateUniqueId();
        team.setId(UUid);
        teamMember.setTeamId(UUid);
        teamMember.setUsername(team.getCreatedUser());
        teamMember.setRole(0);
        teamMapper.insert(team);
        teamMapper.updateHasTeam(team.getCreatedUser(),teamDTO.getHasTeam());
        teamMapper.joinTeam(teamMember);
    }

    @Override
    public TeamVO getHasTeam(String username) {
        Team team = new Team();
        team.setCreatedUser(username);
        TeamVO teamVO = teamMapper.getHasTeam(username);

        return teamVO;
    }

    @Transactional
    public void leaveTeam(TeamDTO teamDTO) {
        // 删除团队成员
        teamMapper.deleteTeamMember(teamDTO.getCreatedUser());
        // 更新员工的 hasTeam 字段为 0
        teamMapper.updateHasTeam(teamDTO.getCreatedUser(), 0);
    }

    @Override
    public PageResult pageQuery(TeamMemberPageQueryDTO teamMemberPageQueryDTO) {
        //开始分页查询
        PageHelper.startPage(teamMemberPageQueryDTO.getPage(),teamMemberPageQueryDTO.getPageSize());

        Page<TeamMember> page = teamMapper.pageQuery(teamMemberPageQueryDTO);
        long total = page.getTotal();
        List<TeamMember> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void RemoveTeamMember(String username) {
        teamMapper.deleteTeamMember(username);
    }

    @Override
    public void update(TeamMemberDTO teamMemberDTO) {
        teamMapper.updateTeamMemberRole(teamMemberDTO.getUsername(),teamMemberDTO.getRole());
    }

    @Override
    @Transactional
    public void CheckTLQuitTeam(TeamDTO teamDTO) {
        String currentUser = teamDTO.getCreatedUser();
        String newCreator = teamDTO.getNewCreator();
        // 更新新创建者的角色为创建者（角色0）
        teamMapper.updateTeamMemberRole(newCreator, 0);
        //删除当前用户的团队成员记录
        teamMapper.deleteTeamMember(currentUser);
        teamMapper.updateTeamCreatedUser(teamDTO.getName(),newCreator);
        //更新当前用户的hasTeam状态
        teamMapper.updateHasTeam(currentUser, 0);
    }

    @Override
    public void deleteTeam(String teamId) {
        //TODO:这个地方要改hasTeam
        //根据teamid获取username,然后再查找employee删除
        teamMapper.deleteTeamWithId(teamId);
    }


    public Long generateUniqueId() {
        // 获取当前时间的毫秒数并转换为字符串
        String timestamp = Long.toString(System.currentTimeMillis());
        // 保持时间戳的长度为9位，这通常是从时间戳中截取最后9位
        String shortenedTimestamp = timestamp.substring(timestamp.length() - 9);
        // 随机数长度固定为3位
        int randomNum = ThreadLocalRandom.current().nextInt(100, 1000);
        // 将截取的时间戳和随机数拼接
        return Long.valueOf(shortenedTimestamp + randomNum);
    }
}
